<template>
  <view class="search-page">
    <view class="search">
      <uni-search-bar :radius="100" cancelButton="none" @input="handleInput" v-model="kw" />
    </view>
    <view class="ghost"></view>
    <view class="suggest" v-if="searchResults.length !== 0">
      <view class="suggest-item" v-for="(item, index) in searchResults" :key="index" @click="toGoodsDetail(item)">
        <text>{{ item.goods_name }}</text>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <view class="history" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="24" @click="clearHistory"></uni-icons>
      </view>
      <view class="history-content">
        <view class="content-item" v-for="(item, index) in historyList" :key="index" @click="toGoodsList(item)">
          {{ item }}
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        searchResults: [],
        kw: '',
        historyList: []
      };
    },
    methods: {
      handleInput() {
        clearTimeout(this.timeoutId)
        this.timeoutId = setTimeout(() => {
          this.getSearchResults()
        }, 1000)
      },
      async getSearchResults() {
        if (this.kw === '') {
          this.searchResults = []
        } else {
          const result = await uni.$http.get(`/api/public/v1/goods/qsearch?query=${this.kw}`)
          if (result.statusCode !== 200) {
            uni.$showMessage()
          } else {
            this.searchResults = result.data.message
          }
          if(this.historyList.every(value => value !== this.kw)) {
            this.historyList.unshift(this.kw)
            uni.setStorageSync('historyList', JSON.stringify(this.historyList))
          }
        }
      },
      toGoodsDetail(item) {
        uni.navigateTo({
          url: `/subpkg/GoodsDetail/GoodsDetail?goods_id=${item.goods_id}`
        })
      },
      clearHistory() {
        this.historyList = []
        uni.removeStorageSync('historyList')
      },
      toGoodsList(item) {
        uni.navigateTo({
          url: `/subpkg/GoodsList/GoodsList?query=${item}`
        })
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('historyList') || '[]')
    }
  }
</script>

<style lang="scss" scoped>
  .search-page {
    position: relative;

    .search {
      position: absolute;
      top: 0;
      left: 0;
      width: 750rpx;
      z-index: 999;
    }

    .ghost {
      height: 56px;
      background-color: transparent;
    }

    .suggest {
      background-color: white;

      .suggest-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20rpx 10rpx;
        border-bottom: 1px solid lightgray;

        text {
          font-size: 24rpx;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
    }
    
    .history {
      background-color: white;
      .history-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20rpx 10rpx;
        border-bottom: 1px solid lightgray;
        
        text {
          font-size: 28rpx;
        }
      }
      .history-content {
        display: flex;
        flex-wrap: wrap;
        .content-item {
          margin: 10rpx;
          padding: 10rpx;
          background-color: #f8f8f8;
        }
      }
    }
  }
</style>